package org.dromara.authcode.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.authcode.domain.vo.TOrderVo;
import org.dromara.authcode.domain.bo.TOrderBo;
import org.dromara.authcode.service.ITOrderService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 订单
 *
 * @author Miles Meng
 * @date 2025-08-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/authcode/order")
public class TOrderController extends BaseController {

    private final ITOrderService tOrderService;

    /**
     * 查询订单列表
     */
    @SaCheckPermission("authcode:order:list")
    @GetMapping("/list")
    public TableDataInfo<TOrderVo> list(TOrderBo bo, PageQuery pageQuery) {
        return tOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单列表
     */
    @SaCheckPermission("authcode:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TOrderBo bo, HttpServletResponse response) {
        List<TOrderVo> list = tOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单", TOrderVo.class, response);
    }

    /**
     * 获取订单详细信息
     *
     * @param orderNo 主键
     */
    @SaCheckPermission("authcode:order:query")
    @GetMapping("/{orderNo}")
    public R<TOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long orderNo) {
        return R.ok(tOrderService.queryById(orderNo));
    }

    /**
     * 新增订单
     */
    @SaCheckPermission("authcode:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TOrderBo bo) {
        return toAjax(tOrderService.insertByBo(bo));
    }

    /**
     * 修改订单
     */
    @SaCheckPermission("authcode:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TOrderBo bo) {
        return toAjax(tOrderService.updateByBo(bo));
    }

    /**
     * 删除订单
     *
     * @param orderNos 主键串
     */
    @SaCheckPermission("authcode:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderNos}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] orderNos) {
        return toAjax(tOrderService.deleteWithValidByIds(List.of(orderNos), true));
    }
}
